package com.example.child_protection.dto.base;

import com.example.child_protection.enums.ResponseCodeEnums;
import com.example.child_protection.util.ToStringBuilder;
import com.example.child_protection.util.ToStringStyle;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * 返回DTO基类
 *
 * @author yxb
 * @date 2018-10-31
 */
@Data
public class BaseResponseDTO {
    private Long id;

    @ApiModelProperty(value = "request是否成功", required = true, readOnly = true, allowableValues = "true,false")
    private boolean success;
    @ApiModelProperty(value = "request执行是否成功", required = true, readOnly = true, allowableValues = "true,false")
    private boolean executed;
    @ApiModelProperty(value = "response编码", required = true, readOnly = true)
    @JsonProperty("code")
    private ResponseCodeEnums responseCode;
    @ApiModelProperty(value = "response消息内容", required = true, readOnly = true)
    private String message;

    private String data;

    public BaseResponseDTO(int code, String message) {
        // this.responseCode = ResponseCodeEnums.SUCCESSFULLY;
        this.message = message;
    }
    public BaseResponseDTO() {
        // this.responseCode = ResponseCodeEnums.SUCCESSFULLY;

    }

    @XmlTransient
    public Long getId() {
        return this.id;
    }

    public BaseResponseDTO setId(Long id) {
        this.id = id;
        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public BaseResponseDTO setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public boolean isExecuted() {
        return this.executed;
    }

    public BaseResponseDTO setExecuted(boolean executed) {
        this.executed = executed;
        return this;
    }

    @XmlElement(name = "code")
    public ResponseCodeEnums getResponseCode() {
        return this.responseCode;
    }

    public BaseResponseDTO setResponseCode(ResponseCodeEnums responseCode) {
        this.responseCode = responseCode;
        return this;
    }

    public String getMessage() {
        return this.message;
    }

    public BaseResponseDTO setMessage(String message) {
        this.message = message;
        return this;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Object getData() {
        return data;
    }

    public BaseResponseDTO setData(String data) {
        this.data = data;
        return this;
    }


    public void handleFailed(ResponseCodeEnums responseCodeEnums) {
        this.setSuccess(false).setExecuted(false).setResponseCode(responseCodeEnums).setMessage(responseCodeEnums.getDisplayName());
    }

    public void successfully() {
        this.setSuccess(true).setExecuted(true).setResponseCode(ResponseCodeEnums.SUCCESSFULLY).setMessage(ResponseCodeEnums.SUCCESSFULLY.getDisplayName());
    }
}
